home *** CD-ROM | disk | FTP | other *** search
- /* OS- and machine-dependent stuff for 4.[23] BSD UNIX */
-
- /*
- FILE: UNIX.io.c
-
- Routines:
- ioinit()
- iostop()
- asy_init()
- asy_stop()
- asy_speed()
- asy_output()
- asy_recv()
- dir()
- Written or converted by Mikel Matthews, N9DVG
-
- If you want to use the select code, define SELECT in the makefile or
- in this file.
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sgtty.h>
- #include <sys/file.h>
- #include <sys/dir.h>
- #include <sys/time.h>
- #include "global.h"
- #include "mbuf.h"
- #include "internet.h"
- #include "iface.h"
- #include "unix.h"
- #include "cmdparse.h"
-
- struct asy asy[ASY_MAX];
-
- struct interface *ifaces;
-
- struct sgttyb mysavetty, savecon;
- int IORser[ASY_MAX];
- /*
- int IOWser;
- */
- char *ttbuf;
-
-
- /* Called at startup time to set up console I/O, memory heap */
- ioinit()
- {
- struct sgttyb ttybuf;
- unsigned grabcore();
- char *malloc();
-
- /* Interrupts use a special stack deep in data space.
- * Calls to sbrk() (invoked by malloc when it needs more memory
- * from the system) at interrupt time will fail because sbrk()
- * will think that the stack has overwritten the heap. So
- * grab all the memory we can now for the heap so that malloc
- * won't have to call sbrk and alloc_mbuf() won't fail unnecessarily
- * at interrupt time.
- */
- ioctl(0,TIOCGETP,&ttybuf);
- savecon = ttybuf;
- ttybuf.sg_flags &= ~ECHO;
- ttybuf.sg_flags |= CBREAK;
- ioctl(0,TIOCSETP,&ttybuf);
- }
- /* Called just before exiting to restore console state */
- iostop()
- {
- struct sgttyb ttybuf;
- setbuf(stdout,NULLCHAR);
- free(ttbuf);
- ioctl(0,TIOCGETP,&ttybuf);
- ttybuf.sg_flags &= ~RAW;
- ioctl(0,TIOCSETP,&ttybuf);
- while(ifaces != NULLIF){
- if(ifaces->stop != NULLFP)
- (*ifaces->stop)(ifaces->dev);
- ifaces = ifaces->next;
- }
- ioctl(0,TIOCSETP,&savecon);
- }
-
- /* Initialize asynch port "dev" */
- int slipisopen;
- int
- asy_init(dev,arg1,arg2,bufsize)
- int16 dev;
- char *arg1,*arg2;
- unsigned bufsize;
- {
- register struct asy *ap;
- extern struct interface *ifaces;
- struct sgttyb sgttyb;
- ap = &asy[dev];
- /*
- printf("asy_init: called\n");
- */
-
- if ( ap == NULL)
- {
- return(-1);
- }
- ap->tty = malloc(strlen(arg2)+1);
- strcpy(ap->tty, arg2);
-
- printf("asy_init: tty name = %s\n", ap->tty);
-
-
- if ((IORser[dev] = open (ap->tty, (O_RDWR), 0)) < 0)
- {
- perror ("Could not open device IORser");
- return (-1);
- }
- /*
- if ((IOWser = open (ap->tty, (O_WRONLY), 0)) < 0)
- {
- perror ("Could not open device IOWser");
- return (-1);
- }
- */
- /*
- * get the stty structure and save it
- */
-
- if (ioctl (IORser[dev], TIOCGETP, &mysavetty) < 0)
- {
- perror ("ioctl failed on device");
- return (-1);
- }
- /*
- * copy over the structure
- */
-
- sgttyb = mysavetty;
- sgttyb.sg_flags = RAW;
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B9600;
-
- /*
- * set up the parity and enter RAW mode
- */
- sgttyb.sg_flags = (RAW | ANYP | CRMOD);
- /*
- * now set the modes and flags
- */
- if (ioctl (IORser[dev], TIOCSETP, &sgttyb) < 0)
- {
- perror ("ioctl could not set parameters for IORser");
- return (-1);
- }
- /*
- if (ioctl (IOWser, TIOCSETP, &sgttyb) < 0)
- {
- perror ("ioctl could not set parameters for IOWser");
- return (-1);
- }
- */
-
- return (0);
- }
-
- int
- asy_stop(dev)
- int dev;
- {
- }
-
- /* Set asynch line speed */
- int
- asy_speed(dev,speed)
- int16 dev;
- int speed;
- {
- struct sgttyb sgttyb;
-
- if(speed == 0 || dev >= nasy)
- return(-1);
- /*
- printf("asy_speed: Setting speed for device %d to %d\n",dev, speed);
- */
- asy[dev].speed = speed;
-
- if (ioctl (IORser[dev], TIOCGETP, &sgttyb) < 0)
- {
- perror ("ioctl could not set parameters");
- return (-1);
- }
- switch(speed)
- {
- case 0:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B0;
- break;
- case 50:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B50;
- break;
- case 75:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B75;
- break;
- case 110:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B110;
- break;
- case 134:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B134;
- break;
- case 150:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B150;
- break;
- case 200:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B200;
- break;
- case 300:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B300;
- break;
- case 600:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B600;
- break;
- case 1200:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B1200;
- break;
- case 1800:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B1800;
- break;
- case 2400:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B2400;
- break;
- case 4800:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B4800;
- break;
- case 9600:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = B9600;
- break;
- /*
- case EXTA:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTA;
- break;
- case EXTB:
- sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTB;
- break;
- */
- default:
- printf("asy_speed: Unknown speed (%d)\n", speed);
- break;
- }
- /*
- printf("speed = %d\n", sgttyb.sg_ispeed);
- */
- if (ioctl (IORser[dev], TIOCSETP, &sgttyb) < 0)
- {
- perror ("ioctl could not set parameters for IORser");
- return (-1);
- }
- /*
- if (ioctl (IOWser, TIOCSETP, &sgttyb) < 0)
- {
- perror ("ioctl could not set parameters for IOWser");
- return (-1);
- }
- */
- return(0);
- }
- /* Send a buffer to serial transmitter */
- asy_output(dev,buf,cnt)
- unsigned dev;
- char *buf;
- unsigned short cnt;
- {
- /*
- printf("asy_output called. dev = %x, cnt = %d\n", dev, cnt);
- printf("buf=%s\n", buf);
- fflush(stdout);
- */
-
- if(dev >= nasy)
- return(-1);
- if ( write(IORser[dev], buf, (int)cnt) < cnt)
- {
- perror("asy_output");
- printf("asy_output: error in writing to device %d\n", dev);
- return(-1);
- }
- return(0);
- }
- /* Receive characters from asynch line
- * Returns count of characters read
- */
- unsigned
- asy_recv(dev,buf,cnt)
- int dev;
- char *buf;
- unsigned cnt;
- {
- unsigned tot;
- long amount;
-
- #ifdef SELECT
- int mask;
- int writemask;
- int ok;
- struct timeval timeout;
- timeout.tv_sec = 0;
- timeout.tv_usec = 35;
- mask = (1<<IORser[dev]);
- writemask = (1<<IORser[dev]);
- ok = 0;
- tot = 0;
- ok = select(mask, &mask, 0, 0, &timeout);
- if ( mask & (1<<IORser[dev]))
- {
- tot = read(IORser[dev], buf, cnt);
- }
- return (tot);
- #else
- tot = 0;
- amount = 0;
- if ( ioctl( IORser[dev], FIONREAD, &amount ) != -1)
- {
- if ( amount > 0)
- {
- /*
- printf("ASY_RECV: amount = %d\n", amount);
- printf("ASY_RECV: cnt = %d\n", cnt);
- */
- tot = read(IORser[dev], buf, (int)cnt);
- /*
- printf("READ %d\n", tot);
- */
- }
- }
- return (tot);
- #endif SELECT
- }
- /* Create a directory listing in a temp file and return the resulting file
- * descriptor. If full == 1, give a full listing; else return just a list
- * of names.
- *
- * This function is very dependent on the workings of Aztec standard I/O;
- * it uses their mechanism for generating and deleting temporary files.
- */
- FILE *
- dir(path,full)
- char *path;
- int full;
- {
- FILE *fp;
- char cmd[50];
- char name[MAXNAMLEN],*tmpnam(),*malloc();
- tmpnam(name);
-
- if ( path[0] == '\0' )
- {
- path = ".";
- /*
- printf("Path = %s\n", path);
- */
- }
- /*
- printf("DIR: tempname = %s, path = %s\n",name, path);
- */
-
- if(full){
- sprintf(cmd,"ls -l %s > %s",path,name);
- system(cmd);
- } else {
- sprintf(cmd, "ls %s > %s", path, name);
- system(cmd);
- }
- if( (fp = fopen(name,"r")) == NULL)
- {
- perror("fopen");
- }
- /* Set up the magic cookies inside the file structure so that the
- * temporary file gets deleted later when the file is closed
- */
- (void)unlink(name);
- return (fp);
- }
- asy_ioctl(interface, argc, argv)
- struct interface *interface;
- int argc;
- char *argv[];
- {
- if (argc < 1) {
- printf("%d\r\n", asy[interface->dev].speed);
- return 0;
- }
- return asy_speed(interface->dev, atoi(argv[0]));
- }
-